package com.jz.community.moduleshoppingguide.nearshop.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopDownStairsBean;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopGoodsActivity;

import java.util.List;


/**
 * Created by Ani_H on 2018/3/6.
 */

public class NearShopGoodsAdapter extends BaseQuickAdapter<ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean, BaseViewHolder> {
    private int screen_width;
    private RoundCornersTransformation transformation;
    private ShopDownStairsBean.EmbeddedBeanX.ContentBean parentEntity;

    public NearShopGoodsAdapter(int layoutResId, @Nullable List<ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean> data, ShopDownStairsBean.EmbeddedBeanX.ContentBean parentEntity) {
        super(layoutResId, data);
        mData = data;
        this.parentEntity = parentEntity;
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean entity) {
        transformation = new RoundCornersTransformation(mContext,
                SHelper.dp2px(mContext, 5), RoundCornersTransformation.CornerType.ALL);
//        //获取屏幕宽高
        WindowManager wm = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        assert wm != null;
        screen_width = wm.getDefaultDisplay().getWidth();//获取屏幕宽度
        LinearLayout goods_parent = helper.getView(R.id.near_shop_goods_parent);
        ImageView imageView = helper.getView(R.id.near_shop_goods_img);
        ViewGroup.LayoutParams lp1 = goods_parent.getLayoutParams();
        int parent_width = ((screen_width - SHelper.dp2px(mContext, 27)) / 7) * 2;
        lp1.width = parent_width;
        goods_parent.setLayoutParams(lp1);
        ViewGroup.LayoutParams lp_img = imageView.getLayoutParams();
        lp_img.width = parent_width - SHelper.dp2px(mContext, 4);
        lp_img.height = parent_width - SHelper.dp2px(mContext, 4);
        imageView.setLayoutParams(lp_img);

        if (helper.getAdapterPosition() != mData.size() - 1) {
            String imga = entity.getIcon();
            GlideApp.with(mContext)
                    .load(imga)
                    .transform(transformation)
                    .placeholder(R.mipmap.image_default)
                    .error(R.mipmap.image_default)
                    .into(imageView);
            helper.setText(R.id.near_shop_goods_name, entity.getTitle());
            if (TextUtils.isEmpty(entity.getDiscountPrice())) {
                helper.setText(R.id.near_shop_goods_price, "¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getPrice())));
            } else {
                helper.setText(R.id.near_shop_goods_price, "¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getDiscountPrice())));
            }
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                intent.putExtra("shopId", parentEntity.getId() + "");
                intent.putExtra("gid", entity.getId() + "");
                mContext.startActivity(intent);
            });
        } else {
            GlideApp.with(mContext)
                    .load(R.mipmap.shop_goods_more)
                    .transform(transformation)
                    .into(imageView);
            imageView.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                intent.putExtra("shopId", parentEntity.getId() + "");
                mContext.startActivity(intent);
            });
        }
    }
}
